package com.reef.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.reef.dto.DetailedDto;
import com.reef.entity.Detailed;
import com.reef.entity.Result;
import com.reef.service.DetailedService;
@RestController
public class DetailedController {
	@Autowired
	private DetailedService deService;
	@RequestMapping(value = "/addDetailed",method = RequestMethod.POST)
	public Object addDetailed(Detailed de) {
		Detailed i = deService.addDetailed(de);
		if(i==null) {
			return new Result("添加失败","",false);
		}else {
			return new Result("添加成功",i,true);
		}
	}
	@RequestMapping(value = "/updateTime",method = RequestMethod.POST)
	public Object updateTime(String endtime, int did) {
		int i = deService.updateTime(endtime, did);
		if (i>0) {
			return new Result("修改成功", i, true);
		}else {
			return new Result("修改失败", "", false);
		}
	}
	/**
	 * 多条件动态查询
	 */
	@RequestMapping("/findByDetailedCondition")
	public Object findByDetailedCondition(DetailedDto dDto,int page,int limit) {
		//getTotalElements()指实体中有多少元素
		//分页查询 springBoot2.0以下版本用new 实例化，2.0以上版本使用的是PageRequest
		Pageable pageable = PageRequest.of(page-1, limit);
		Page<Detailed> pagelist  = deService.findByDetailedCondition(dDto, pageable);	
		  Map<String, Object> map =new HashMap<String, Object>();
		  map.put("code", 0);
		  map.put("msg", "");
		  map.put("count", pagelist.getTotalElements());
		  map.put("data", pagelist.getContent());
		 //你写的模块在那个地方
		return map;
	}
}
